/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package upeu.cepre.business;

import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import upeu.cepre.model.Alumno;
import upeu.cepre.model.DetalleAlumnoExamen;
import upeu.cepre.model.Examenes;

import upeu.cepre.util.DataBaseConexion;

/**
 *
 * @author Elvis Zanguetsu
 */
public class DetalleAlumnoExamenManager {

    public Connection connection;

    public List<DetalleAlumnoExamen> Report() {
        List<DetalleAlumnoExamen> detalleAlumnoExamenes = new ArrayList<DetalleAlumnoExamen>();


        try {

            connection = DataBaseConexion.getConexion();
            Statement statement = connection.createStatement();
            PreparedStatement prepareestatemente = connection.prepareStatement("SELECT * FROM  detallealumnoexamen");


            ResultSet rs = prepareestatemente.executeQuery();
            while (rs.next()) {
                DetalleAlumnoExamen detalleAlumnoExamen = new DetalleAlumnoExamen();


                detalleAlumnoExamen.setIdDetalleAlumnoExamen(rs.getString("idDetalleAlumnoExamen"));
                detalleAlumnoExamen.setIdExamenes(rs.getString("idExamenes"));
                detalleAlumnoExamen.setNotaExamen(rs.getString("notaExamen"));
                detalleAlumnoExamen.setIdDetallePeriodoAlumno(rs.getString("idDetallePeriodoAlumno"));

                detalleAlumnoExamenes.add(detalleAlumnoExamen);

            }
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return detalleAlumnoExamenes;
    }

    public void Add(String idDetallePeriodoAlumno, String periodo) {
        try {
            ExamenesManger examenesManag = new ExamenesManger();
            List<Examenes> listExamenes = new ArrayList<Examenes>();
            listExamenes = examenesManag.findByIdPeriodo(periodo);
            DetalleAlumnoExamenManager alumnoExamenManag = new DetalleAlumnoExamenManager();

            for (int y = 0; y < listExamenes.size(); y++) {
                System.out.println("---  tamaño examenes  ---");
                System.out.println("---  " + y + "  ---");
                connection = DataBaseConexion.getConexion();
                PreparedStatement preparedStatement =
                        connection.prepareStatement("INSERT INTO detallealumnoexamen (idDetalleAlumnoExamen, idExamenes, notaExamen , idDetallePeriodoAlumno ) VALUES (?, ?, ?, ? ) ");

                preparedStatement.setString(1, alumnoExamenManag.ObtenerId());
                preparedStatement.setString(2, listExamenes.get(y).getIdExamenes());
                preparedStatement.setString(3, "00");
                preparedStatement.setString(4, idDetallePeriodoAlumno);


                preparedStatement.executeUpdate();
                connection.close();

            }
        } catch (SQLException e) {
            e.printStackTrace();
        }


    }

    public String ObtenerId() {

        java.util.Date ahora = new java.util.Date();
        SimpleDateFormat formateador = new SimpleDateFormat("ddMMyyyy");
        DetalleAlumnoExamenManager trabManager = new DetalleAlumnoExamenManager();
        int a = trabManager.Report().size();
        return formateador.format(ahora) + "AlumnoExamen" + (a + 1);

    }

    public void UpdateNotas(String idDetalleAlumnoExamen, String notaExamen) {
        try {

            connection = DataBaseConexion.getConexion();
            Statement statement = connection.createStatement();
            PreparedStatement preparedStatement = connection.prepareStatement(
                    "UPDATE detallealumnoexamen SET notaExamen=? WHERE idDetalleAlumnoExamen=?");
            // Parameters start with 1

            preparedStatement.setString(1, notaExamen);
            preparedStatement.setString(2, idDetalleAlumnoExamen);

            preparedStatement.executeUpdate();
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("---  " + e + "  ---");

        }
    }

    public List<DetalleAlumnoExamen> findByIdPeriodoAlumno(String idDetallePeriodoAlumno) {
        List<DetalleAlumnoExamen> detalleAlumnoExamenes = new ArrayList<DetalleAlumnoExamen>();

        try {
            connection = DataBaseConexion.getConexion();
            Statement statement = connection.createStatement();
            PreparedStatement prepareestatemente = connection.prepareStatement("SELECT * FROM  detallealumnoexamen WHERE idDetallePeriodoAlumno=?  ");
            prepareestatemente.setString(1, idDetallePeriodoAlumno);
            ResultSet rs = prepareestatemente.executeQuery();
            while (rs.next()) {

                DetalleAlumnoExamen detalleAlumnoExamen = new DetalleAlumnoExamen();

                detalleAlumnoExamen.setIdDetalleAlumnoExamen(rs.getString("idDetalleAlumnoExamen"));
                detalleAlumnoExamen.setIdExamenes(rs.getString("idExamenes"));
                detalleAlumnoExamen.setNotaExamen(rs.getString("notaExamen"));
                detalleAlumnoExamen.setIdDetallePeriodoAlumno(rs.getString("idDetallePeriodoAlumno"));



                detalleAlumnoExamenes.add(detalleAlumnoExamen);

            }

            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return detalleAlumnoExamenes;
    }

    public void Add(DetalleAlumnoExamen detalleAlumnoExamenes) {
        try {
            connection = DataBaseConexion.getConexion();
            PreparedStatement preparedStatement =
                    connection.prepareStatement(
                    "  INSERT INTO detallealumnoexamen ( idDetalleAlumnoExamen , idExamenes , notaExamen , idDetallePeriodoAlumno ) VALUES (?,?,?,?)");

            preparedStatement.setString(1, detalleAlumnoExamenes.getIdDetalleAlumnoExamen());
            preparedStatement.setString(2, detalleAlumnoExamenes.getIdExamenes());
            preparedStatement.setString(3, detalleAlumnoExamenes.getNotaExamen());
            preparedStatement.setString(4, detalleAlumnoExamenes.getIdDetallePeriodoAlumno());
            preparedStatement.executeUpdate();
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }


    }
}
